package com.timedomain.him.remake.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;

// 设置系统，通过反射读取和保存所有的字段。
public class SettingUtils {

    public static void loadAllSetting(Context ctx,Class clazz) throws IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        SharedPreferences sp = ctx.getSharedPreferences(clazz.getSimpleName(),Context.MODE_PRIVATE);
        for (Field field: fields ) {
            if(Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers())){
                if(field.getType() == boolean.class){
                    field.set(null,sp.getBoolean(field.getName(),field.getBoolean(null)));
                    continue;
                }
                if(field.getType() == float.class){
                    field.set(null,sp.getFloat(field.getName(),field.getFloat(null)));
                    continue;
                }
                if(field.getType() == int.class){
                    field.set(null,sp.getInt(field.getName(),field.getInt(null)));
                    continue;
                }
                if(field.getType() == long.class){
                    field.set(null,sp.getLong(field.getName(),field.getLong(null)));
                    continue;
                }
                if(field.getType() == String.class){
                    field.set(null,sp.getString(field.getName(),field.get(null) == null ? null : field.get(null).toString()));
                    continue;
                }
                if(field.getType() == (HashSet.class)){
                    HashSet<String> set = new HashSet<>(sp.getStringSet(field.getName(),new HashSet<>()));
                    field.set(null,set);
                    continue;
                }
                throw new IllegalArgumentException("Invalid member "+field.getName() +" with type "+field.getType().getSimpleName());
            }
        }
    }

    public static void saveAllSetting(Context ctx,Class clazz) throws IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        SharedPreferences sp = ctx.getSharedPreferences(clazz.getSimpleName(),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for (Field field: fields ) {
            if(Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers())){
                if(field.getType() == boolean.class){
                    editor.putBoolean(field.getName(),field.getBoolean(null));
                    continue;
                }
                if(field.getType() == float.class){
                    editor.putFloat(field.getName(),field.getFloat(null));
                    continue;
                }
                if(field.getType() == int.class){
                    editor.putInt(field.getName(),field.getInt(null));
                    continue;
                }
                if(field.getType() == long.class){
                    editor.putLong(field.getName(),field.getLong(null));
                    continue;
                }
                if(field.getType() == String.class){
                    editor.putString(field.getName(),field.get(null) == null ? null : field.get(null).toString());
                    continue;
                }
                if(field.getType() == (HashSet.class)){
                    editor.putStringSet(field.getName(),(HashSet<String>)field.get(null));
                    continue;
                }
                throw new IllegalArgumentException("Invalid member "+field.getName() +" with type "+field.getType().getSimpleName());
            }
        }
        editor.apply();
    }

}
